package com.sdy.window;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sdy.SerialFactory;
import com.sdy.po.SerialPO;
import com.sdy.utils.HttpClientUtil;
import com.sdy.utils.MyWebSocketClient;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URISyntaxException;

public class MainWindow extends JFrame {

    private JButton b1;

    private JTextField t1;

    private JTextField t2;

    private JLabel j1;

    private JLabel j2;

    private JLabel j3;

    private SerialFactory serialFactory;


    //定义构造器
    public MainWindow(){
        this.setLayout(null);	//设置布局管理器
        setTitle("Serial_WS.exe");// 窗体的标题
//        JButton b1= new JButton();
        b1 = new JButton();
        t1= new JTextField();
        t2= new JTextField();
        j1 = new JLabel();	//定义一个标签
        j2 = new JLabel();	//定义一个标签
        j3 = new JLabel();	//定义一个标签
        j1.setText("企业识别码");	//设置显示文字
        j1.setBounds(70,40,150,20);	//设置标签的大小和位置
        t1.setText("");
        t1.setBounds(170,30,230,40);	//设置标签的大小和位置
        j2.setText("服务器IP");	//设置显示文字
        j2.setBounds(85,100,150,20);	//设置标签的大小和位置
        t2.setText("");
        t2.setBounds(170,90,230,40);	//设置标签的大小和位置
        b1.setText("确定");
        b1.setBounds(210,150,80,40);	//设置标签的大小和位置
        j3.setText("");
        j3.setBounds(85,180,200,20);
        this.add(t1);
        this.add(t2);
        this.add(b1);
        this.add(j1);
        this.add(j2);
        this.add(j3);
        this.setBounds(200,250,500,260);	//设置窗口的大小和位置
        this.setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sureButton();
            }
        });
    }

    public JLabel getJ3() {
        return j3;
    }

    public void setJ3(JLabel j3) {
        this.j3 = j3;
    }

    public JButton getB1() {
        return b1;
    }

    public void setB1(JButton b1) {
        this.b1 = b1;
    }

    public JTextField getT1() {
        return t1;
    }

    public void setT1(JTextField t1) {
        this.t1 = t1;
    }

    public JTextField getT2() {
        return t2;
    }

    public void setT2(JTextField t2) {
        this.t2 = t2;
    }

    public JLabel getJ1() {
        return j1;
    }

    public SerialFactory getSerialFactory() {
        return serialFactory;
    }

    public void setSerialFactory(SerialFactory serialFactory) {
        this.serialFactory = serialFactory;
    }

    public void setJ1(JLabel j1) {
        this.j1 = j1;
    }

    public JLabel getJ2() {
        return j2;
    }

    public void setJ2(JLabel j2) {
        this.j2 = j2;
    }

    private void sureButton() {

        serialFactory = new SerialFactory();
        JSONObject serialData  = new JSONObject();

        try {
            serialData = JSON.parseObject(HttpClientUtil.sendGet("http://"+this.t2.getText()+":8088/getSerials?code="+this.t1.getText()));
            serialFactory.setWebSocketClient(new MyWebSocketClient("ws://"+this.t2.getText()+":8088/websocket/serialData/"+this.t1.getText()));
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "服务器IP不正确...... ");
            e.printStackTrace();
            return;
        } catch (URISyntaxException e) {
            JOptionPane.showMessageDialog(null, "WebSocket连接失败...... ");
            e.printStackTrace();
        }

        if ((Boolean)serialData.get("result")){
            serialFactory.setSerialList(JSON.parseArray(serialData.get("data").toString(), SerialPO.class));
        }
        else {
            JOptionPane.showMessageDialog(null, "识别码错误或未在本公司注册串口");
            return;
        }

        new InfoWindow(serialFactory,serialFactory.initSerials());

    }
}
